package com.newasia.server.common.template;

import com.alibaba.fastjson.JSON;
import com.newasia.server.common.template.dataItem.ProtocolFormat;
import com.newasia.server.common.template.dataItem.basics.DataType;
import jodd.util.StringUtil;

import java.io.*;
import java.util.*;

/**
 *协议解析
 * @author LiChong
 * @date 2020/4/21
 */
public class ConfigReader {

    public static ProtocolFormat doLoadConfig(Long moduleId) {
        String srcFileName = "C:\\configs\\NA"+moduleId+".ini";
        System.out.println(moduleId);
        List<DataType> dataList = new ArrayList<DataType>(200);
        List<DataType> setupList = new ArrayList<DataType>(200);
        BufferedReader reader = null;
        try{
            File file = new File(srcFileName);
            if(!file.isFile()) {
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            final StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            String line = null;
            String flag = null;
            while ((line = reader.readLine()) != null) {
                if ("".equals(line))
                    continue;
                if (line.startsWith("[") && line.endsWith("]")) {
                    String dataItem = line.substring(1,line.length()-1);
                    flag = dataItem;
                    while (true) {
                        //衡量是否为数据项
                        String upDataItem = flag.toUpperCase();
                        if ((!upDataItem.equals("DATASHOW") &&
                                !upDataItem.equals("SETUPSHOW") &&
                                !upDataItem.equals("STARTSTOPSETUP")) &&
                                ((upDataItem.contains("DATA") && upDataItem.length() > 4) ||
                                        ((upDataItem.contains("SETUP") && upDataItem.length() > 5)))) {
                            final StringBuffer dataStringBuffer = new StringBuffer();
                            dataStringBuffer.append("{");
                            while ((line = reader.readLine()) != null) {
                                line = line.trim();
                                if(line.contains("[")){
                                    flag = line.substring(1,line.length()-1);
                                    break;
                                }
                                if ("".equals(line))
                                    continue;
                                if (line.contains("=")) {
                                    line = "\"" + line + "\",";
                                    dataStringBuffer.append(line);
                                }
                            }
                            dataStringBuffer.append("\"\":\"\"}");
                            String result = dataStringBuffer.toString()
                                    .replaceAll("=", "\":\"")
                                    .replaceAll(",!", "},")
                                    .replaceAll("!", "");
                            DataType dataType = JSON.parseObject(result, DataType.class);
                            if (upDataItem.contains("DATA")) {
                                Integer id = Integer.valueOf(upDataItem.substring(4, upDataItem.length()));
                                dataType.setId(id);
                                dataList.add(dataType);
                            } else {
                                Integer id = Integer.valueOf(upDataItem.substring(5, upDataItem.length()));
                                dataType.setId(id);
                                setupList.add(dataType);
                            }
                            if(StringUtil.equals(flag.toUpperCase(),upDataItem)){
                                break;
                            }
                            continue;
                        }else{
                            break;
                        }
                    }
                    line = "!\"" + flag + "\":{";
                    stringBuffer.append(line);
                    continue;
                }
                if(line.contains("=")){
                    line= "\""+line+"\",";
                    stringBuffer.append(line);
                }
            }
            stringBuffer.append("\"\":\"\"}}");
            String result = stringBuffer.toString()
                    .replaceAll("=","\":\"")
                    .replaceAll(",!","},")
                    .replaceAll("!","");

            ProtocolFormat protocolFormat = JSON.parseObject(result,ProtocolFormat.class);
            Comparator<DataType> comparator = new Comparator<DataType>() {
                @Override
                public int compare(DataType data1, DataType data2) {
                    return data1.getId().compareTo(data2.getId());
                }
            };
            dataList.sort(comparator);
            setupList.sort(comparator);
            protocolFormat.setDataList(dataList,protocolFormat.getData().getDataCount());
            protocolFormat.setSetupList(setupList,protocolFormat.getSetup().getSetupCount());
            reader.close();
            return protocolFormat;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }
                catch (IOException e1){
                    e1.printStackTrace();
                }
            }
        }
        return null;
    }
}
